// pages/aduting/retreat/retreat_opration.js
var util = require('../../../utils/util.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hiddenmodalput: true,
    isAgree: false,
    superLus: '0/100',
    deductions: '0.00',
    decratibe: '',
    hiddenmodalre: true,
    subZj:'0.00',
    subYj: '0.00',
    refuse_reson:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.getStorage({
      key: 'userInfo',
      success: function (res) {
        that.setData({
          userInfo: res.data,
          id: options.id
        })
        util.getReq("/app/my/getChangeRoomShenHeDetail", {
          source: that.data.userInfo.source,
          uid: that.data.userInfo.user_id,
          'order.order_id': that.data.id,
          'user.source': that.data.userInfo.source,
          'user.user_id': that.data.userInfo.uid,
          'user.user_name': that.data.userInfo.user_name
        }, function (datas) {
          console.log(datas);
          if (datas.code == 0) {
            var list = datas.params.list;// 账单
            var newOrder = datas.params.newOrder;// 转入房源信息
            var oldOrder = datas.params.oldOrder;// 当前房源信息
            var dayNum = datas.params.dayNum;
            var monthDay = datas.params.monthDay;
            // 新房第一个月如果是一个整月，则算它一个整月的租金。如果不是则第一期的第一天不用算租金。
            var firstRent = datas.params.isOneDay == 1 ? newOrder.month_rent : newOrder.month_rent * dayNum / monthDay;
            var yingFu = firstRent + (newOrder.month_rent * (newOrder.rent_num - 1));
            yingFu = (yingFu * 1).toFixed(2);
            var obegin_lease_time = util.format(oldOrder.begin_lease_time, 'yyyy-MM-dd');
            var oplan_end_time = util.format(oldOrder.plan_end_time, 'yyyy-MM-dd');
            var nbegin_lease_time = util.format(newOrder.change_room_time,'yyyy-MM-dd');
            var nplan_end_time = util.format(newOrder.plan_end_time,'yyyy-MM-dd');
            var apply_time = util.format(newOrder.apply_time,'yyyy-MM-dd');
            var change_room_time = util.format(newOrder.change_room_time,'yyyy-MM-dd');
            var old_deposit_num = oldOrder.deposit_num == -1 ? '自定义' : oldOrder.deposit_num;
            var new_deposit_num = newOrder.deposit_num == -1 ? '自定义' : newOrder.deposit_num;
            var yiFu = datas.params.yiFu.toFixed(2);
            var cyingFu = datas.params.cyingFu.toFixed(2);
            var sum = oldOrder.init_deposit * 1 + datas.params.yiFu * 1;// 要付的金额
            var sub = newOrder.month_depost * 1 + yingFu * 1 + cyingFu * 1;// 要扣的金额
            if (newOrder.manage_cost > 0) {
            
              sub += newOrder.manage_cost * newOrder.rent_num;
            }
            if (newOrder.network_cost > 0) {
            
              sub += newOrder.network_cost * newOrder.rent_num;
            }
            if (newOrder.custom_cost > 0) {
              sub += newOrder.custom_cost * newOrder.rent_num;
            }
           for(var i=0;i<list.length;i++){            
              sub += list[i].sumMoney * 1;
            }
          var jg = (sum - sub).toFixed(2);// 计算结果
          var num1 = Number(newOrder.month_depost - oldOrder.init_deposit).toFixed(2);
          var num2 = Number(newOrder.month_rent - oldOrder.init_rent).toFixed(2);
            that.setData({
              newOrder: newOrder,
              oldOrder: oldOrder,
              dayNum: dayNum,
              monthDay: monthDay,
              firstRent: firstRent,
              yingFu: yingFu,
              yingFu2: yingFu,
              obegin_lease_time: obegin_lease_time,
              oplan_end_time: oplan_end_time,
              nbegin_lease_time: nbegin_lease_time,
              nplan_end_time: nplan_end_time,
              apply_time: apply_time,
              change_room_time:change_room_time,
              old_deposit_num:old_deposit_num,
              new_deposit_num:new_deposit_num,
              yiFu: yiFu,
              cyingFu: cyingFu,
              jg: jg,
              jg2: jg,
              list: list,
              change_room_id:newOrder.change_room_id,
              subYj:num1,
              subZj: num2,
              yfyj: newOrder.month_depost,
              total_money:0.00,
              month_depost: newOrder.month_depost,
              month_rent:newOrder.month_rent
            })
          } else {
            wx.showToast({
              title: datas.desc,
            })
          }
        });

      }, fail: function () {
        wx.redirectTo({
          url: '/pages/index/index',
        })
      }
    });
  },

  bindAgreeChange: function (e) {
    console.log(e.detail.value);
    this.setData({
      isAgree: !!e.detail.value.length
    });
  },
  bindSuperLus: function (e) {
    if (e.detail.value.length <= 100) {
      this.setData({
        decratibe: e.detail.value
      })
    }

  },
  bindAgree: function (e) {
    var title = '换房时间为【' + this.data.change_room_time + '】,本操作说明您与租户之间的换房费用已【线下结清】，以上流水记录为【现金流水】。旧房间立即变为可出租状态';
    if (!this.data.isAgree){

        title="确定同意换房吗？";
    }

    this.setData({
      hiddenmodalput: false,
      tipes: title
    })
    


  },
  //取消同意
  cancelM: function (e) {
    this.setData({

      hiddenmodalput: true
    })
  },
  //同意换房
  confirmM: function (e) {
    this.setData({
      hiddenmodalput: true,
    })
    
    var url ="agreeChangeRoomUnderLine";
    if (!this.data.isAgree){
      url ="agreeChangeRoomOnLine";

    }
    var regNeg = /^[1-9]+\d*(\.\d{0,2})?$|^0?\.\d{0,2}$/;  //浮点数
      if (!regNeg.test(this.data.deductions) || !regNeg.test(this.data.subYj) ||              !regNeg.test(this.data.subZj)){
              wx.showModal({
                title:'错误',
                content:"金额错误",
                mode: true,
                showCancel: false,
                confirmColor: '#007aff',
              })
              return false;
            }

  
    wx.showLoading({
      title: '加载中请稍后',
      mask: true,
    })
    
    util.getReq("/app/my/"+url, {
      source: this.data.userInfo.source,
      uid: this.data.userInfo.user_id,
      'order.order_id': this.data.id,
      "order.change_room_id":this.data.change_room_id,
      "order.deposit_growth_money": this.data.subYj,
      "order.rent_growth_money": this.data.subZj,
      'user.source': this.data.userInfo.source,
      'user.user_id': this.data.userInfo.user_id,
      'user.user_name': this.data.userInfo.user_name,
      'xy': this.data.isAgree,
      'reading.total_money': this.data.deductions,
      "reading.remark": this.data.decratibe,
      "jg": this.data.jg,
      "order.change_room_time": this.data.change_room_time
    }, function (datas) {
      if (datas.code == 0) {
        wx.hideLoading();
        wx.navigateBack({
          delta: 2
          //url: '/pages/aduting/hf/hf_list',
        })
      } else if (datas.code == 301) {
        wx.hideLoading();
        wx.redirectTo({
          url: '/pages/index/index',
        })
      } else {
        wx.hideLoading();
        wx.showModal({
          title: '错误',
          content: datas.desc,
          mode: true,
          showCancel: false,
          confirmColor: '#007aff',
        })
      }
    });
  },
  //拒绝换房
  bindRefuse: function (e) {
    this.setData({
      hiddenmodalre: false

    })

  },
  //拒绝换房确定
  reconfirmM: function (e) {
    this.setData({
      hiddenmodalre: true,
    })
   
    wx.showLoading({
      title: '加载中请稍后',
      mask: true,
    })
    util.getReq("/app/my/refuseChangeRoom", {
      source: this.data.userInfo.source,
      uid: this.data.userInfo.uid,
      'order.order_id': this.data.id,
      "order.change_room_id": this.data.change_room_id,
      "order.deposit_growth_money": this.data.subYj,
      "order.rent_growth_money": this.data.subZj,
      'user.source': this.data.userInfo.source,
      'user.user_id': this.data.userInfo.user_id,
      'user.user_name': this.data.userInfo.user_name,
      'xy': this.data.isAgree,
      "order.reject_cause": this.data.refuse_reson
    }, function (datas) {
      if (datas.code == 0) {
        wx.hideLoading();
        wx.navigateBack({
          //url: '/pages/aduting/hf/hf_list',
          delta: 2
        })
      } else if (datas.code == 301) {
        wx.hideLoading();
        wx.redirectTo({
          url: '/pages/index/index',
        })
      } else {
        wx.hideLoading();
        wx.showToast({

          title: datas.desc,
        })
      }
    });
  },
  //拒绝换房取消
  recancelM: function (e) {
    this.setData({
      hiddenmodalre: true

    })

  },
  refuse_reson: function (e) {
    this.setData({
      refuse_reson: e.detail.value

    })
  },
  bindChangeY:function(e){
    var month_rent = this.data.month_rent * 1;// 新租金
    var month_depost = e.detail.value * 1;// 新押金
    var jg = this.data.jg2 * 1;// 原结果
    var zj = this.data.newOrder.month_rent * 1;// 原租金
    var yj = this.data.oldOrder.init_deposit * 1;// 原押金
    var old_yingFu = this.data.yingFu2 * 1;// 新房的原应付租金
    var rent_num = this.data.newOrder.rent_num * 1;// 原现付几个月的房租
    var subZj = 0;// 租金差额
    var subYj = 0;// 押金差额
    var monthDay = this.data.monthDay;// 换房当月总共有多少天
    var dayNum = this.data.dayNum;// 换房当月住了多少天
  
    var yingFu = (month_rent * dayNum / monthDay) + (month_rent * (rent_num - 1));// 新房的新应付租金
    this.setData({
      yingFu: yingFu.toFixed(2),// 新房应付押金
      yfyj: month_depost,
      yfzj: yingFu
    })
   // $("#yfzj").text(yingFu.toFixed(2) + '');// 新房应付押金
    var total_money = this.data.total_money * 1;// 违约扣款
   
    if (month_rent > zj) {// 新租金大于原租金
      subZj = (month_rent - zj).toFixed(2);
      jg -= (yingFu - old_yingFu);
    } else {
      subZj = (month_rent - zj);
      jg += (old_yingFu - yingFu);

    }
    if (month_depost > yj) {// 新押金大于原押金
      subYj = (month_depost - yj);
      jg -= subYj;
    } else {
      subYj = (month_depost - yj);
      jg -= subYj;
    }
    jg -= total_money;
    //var jg = $("#jg").text(jg.toFixed(2) + '');
    this.setData({
      subYj: Number(subYj).toFixed(2),
      jg: jg.toFixed(2),
      month_depost: month_depost
    })
  },


  bindChangeZ:function(e){
    var month_rent = e.detail.value * 1;// 新租金
    var month_depost = this.data.month_depost * 1;// 新押金
    var jg = this.data.jg2 * 1;// 原结果
    var zj = this.data.newOrder.month_rent * 1;// 原租金
    var yj = this.data.oldOrder.init_deposit * 1;// 原押金
    var old_yingFu = this.data.yingFu2 * 1;// 新房的原应付租金
    var rent_num = this.data.newOrder.rent_num * 1;// 原现付几个月的房租
    var subZj = 0;// 租金差额
    var subYj = 0;// 押金差额
    var monthDay = this.data.monthDay;// 换房当月总共有多少天
    var dayNum = this.data.dayNum;// 换房当月住了多少天

    var yingFu = (month_rent * dayNum / monthDay) + (month_rent * (rent_num - 1));// 新房的新应付租金
    this.setData({
      yingFu: yingFu.toFixed(2),// 新房应付押金
      yfyj: month_depost,
      yfzj: yingFu
    })
    // $("#yfzj").text(yingFu.toFixed(2) + '');// 新房应付押金
    var total_money = this.data.total_money * 1;// 违约扣款

    if (month_rent > zj) {// 新租金大于原租金
      subZj = (month_rent - zj).toFixed(2);
      jg -= (yingFu - old_yingFu);
    } else {
      subZj = (month_rent - zj);
      jg += (old_yingFu - yingFu);

    }
    if (month_depost > yj) {// 新押金大于原押金
      subYj = (month_depost - yj);
      jg -= subYj;
    } else {
      subYj = (month_depost - yj);
      jg -= subYj;
    }
    jg -= total_money;
    //var jg = $("#jg").text(jg.toFixed(2) + '');
    this.setData({
      subZj: Number(subZj).toFixed(2),
      jg: jg.toFixed(2),
      month_rent: month_rent
    })

  },
  bindDeductions: function (e) {
    var month_rent = this.data.month_rent * 1;// 新租金
    var month_depost = this.data.month_depost * 1;// 新押金
    var jg = this.data.jg2 * 1;// 原结果
    var zj = this.data.newOrder.month_rent * 1;// 原租金
    var yj = this.data.oldOrder.init_deposit * 1;// 原押金
    var old_yingFu = this.data.yingFu2 * 1;// 新房的原应付租金
    var rent_num = this.data.newOrder.rent_num * 1;// 原现付几个月的房租
    var subZj = 0;// 租金差额
    var subYj = 0;// 押金差额
    var monthDay = this.data.monthDay;// 换房当月总共有多少天
    var dayNum = this.data.dayNum;// 换房当月住了多少天

    var yingFu = (month_rent * dayNum / monthDay) + (month_rent * (rent_num - 1));// 新房的新应付租金
    this.setData({
      yingFu: yingFu.toFixed(2),// 新房应付押金
      yfyj: month_depost,
      yfzj: yingFu
    })
    // $("#yfzj").text(yingFu.toFixed(2) + '');// 新房应付押金
    console.log(e.detail.value * 1);
    var total_money = e.detail.value * 1;// 违约扣款

    if (month_rent > zj) {// 新租金大于原租金
      subZj = (month_rent - zj).toFixed(2);
      jg -= (yingFu - old_yingFu);
    } else {
      subZj = (month_rent - zj);
      jg += (old_yingFu - yingFu);

    }
    if (month_depost > yj) {// 新押金大于原押金
      subYj = (month_depost - yj);
      jg -= subYj;
    } else {
      subYj = (month_depost - yj);
      jg -= subYj;
    }
    jg -= total_money;
    //var jg = $("#jg").text(jg.toFixed(2) + '');
    this.setData({
      jg: jg.toFixed(2),
      total_money: total_money
    })

  },

})